package com.badlogic.agdwikisamples.utils;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.widget.ScrollView;
import android.widget.TextView;

public abstract class BenchmarkActivity extends Activity
{
	long start = 0;
	ScrollView sv;
	TextView tv;	
	Thread testThread;
	String log = "";
	
	public void onCreate( Bundle bundle )
	{
		super.onCreate( bundle );
	
		tv = new TextView( this );
		tv.setText( "Starting benchmark...\n" );
		sv = new ScrollView( this );
		sv.addView( tv );
		setContentView( sv );
		
		setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT );
	}
	
	public void onResume( )
	{
		super.onResume( );
		testThread = new Thread( new Runnable( ) {

			@Override
			public void run()
			{
				benchmark( );
			}
		});
		testThread.start();
	}
	
	public void onPause( )
	{
		super.onPause( );
		
		try
		{
			testThread.stop();
		}
		catch( Exception ex )
		{
			// yeah i know, this is bad :)
		}
	}
	
	protected void tic( )
	{
		start = System.nanoTime();
	}
	
	protected float toc( final String info )
	{
		final float time = (System.nanoTime()-start)/1000000000.0f;
		
		tv.post( new Runnable() {

			@Override
			public void run()
			{
				StringBuilder buff = new StringBuilder( tv.getText() );
				buff.append( info ).append( ", " ).append(time).append( " secs\n" );
				tv.setText( buff.toString() );
				log = buff.toString();
			}
		} );
		
		return time;
	}
	
	protected void info( final String info )
	{
		tv.post( new Runnable() {

			@Override
			public void run()
			{
				StringBuilder buff = new StringBuilder( tv.getText() );
				buff.append( info ).append( "\n" );
				tv.setText( buff.toString() );
				log = buff.toString();
			}
		} );
	}
	
	public String getLog( )
	{
		return log;
	}
	
	public abstract void benchmark( );
}
